<?php
/**
 * Class that operate on table 'registeration'. Database Mysql.
 *
 * @author: http://phpdao.com
 * @date: 2015-05-26 14:36
 */
class RegisterationMySqlDAO implements RegisterationDAO{

	/**
	 * Get Domain object by primry key
	 *
	 * @param String $id primary key
	 * @return RegisterationMySql 
	 */
	public function load($id){
		$sql = 'SELECT * FROM registeration WHERE register_id = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($id);
		return $this->getRow($sqlQuery);
	}

	/**
	 * Get all records from table
	 */
	public function queryAll(){
		$sql = 'SELECT * FROM registeration';
		$sqlQuery = new SqlQuery($sql);
		return $this->getList($sqlQuery);
	}
	
	/**
	 * Get all records from table ordered by field
	 *
	 * @param $orderColumn column name
	 */
	public function queryAllOrderBy($orderColumn){
		$sql = 'SELECT * FROM registeration ORDER BY '.$orderColumn;
		$sqlQuery = new SqlQuery($sql);
		return $this->getList($sqlQuery);
	}
	
	/**
 	 * Delete record from table
 	 * @param registeration primary key
 	 */
	public function delete($register_id){
		$sql = 'DELETE FROM registeration WHERE register_id = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($register_id);
		return $this->executeUpdate($sqlQuery);
	}
	
	/**
 	 * Insert record to table
 	 *
 	 * @param RegisterationMySql registeration
 	 */
	public function insert($registeration){
		$sql = 'INSERT INTO registeration (first_name, last_name, email, phone, city_name, state_id, country_id, address, fax, company, photo, zip, user_id, created_by, create_date, updated_by, updated_date, status) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)';
		$sqlQuery = new SqlQuery($sql);
		
		$sqlQuery->set($registeration->firstName);
		$sqlQuery->set($registeration->lastName);
		$sqlQuery->set($registeration->email);
		$sqlQuery->set($registeration->phone);
		$sqlQuery->set($registeration->cityName);
		$sqlQuery->setNumber($registeration->stateId);
		$sqlQuery->setNumber($registeration->countryId);
		$sqlQuery->set($registeration->address);
		$sqlQuery->set($registeration->fax);
		$sqlQuery->set($registeration->company);
		$sqlQuery->set($registeration->photo);
		$sqlQuery->setNumber($registeration->zip);
		$sqlQuery->setNumber($registeration->userId);
		$sqlQuery->setNumber($registeration->createdBy);
		$sqlQuery->set($registeration->createDate);
		$sqlQuery->setNumber($registeration->updatedBy);
		$sqlQuery->set($registeration->updated_date);
		$sqlQuery->setNumber($registeration->status);

		$id = $this->executeInsert($sqlQuery);	
		$registeration->registerId = $id;
		return $id;
	}
	
	/**
 	 * Update record in table
 	 *
 	 * @param RegisterationMySql registeration
 	 */
	public function update($registeration){
		$sql = 'UPDATE registeration SET first_name = ?, last_name = ?, email = ?, phone = ?, city_name = ?, state_id = ?, country_id = ?, address = ?, fax = ?, company = ?, photo = ?, zip = ?, user_id = ?, created_by = ?, create_date = ?, updated_by = ?, updated_date = ?, status = ? WHERE register_id = ?';
		$sqlQuery = new SqlQuery($sql);
		
		$sqlQuery->set($registeration->firstName);
		$sqlQuery->set($registeration->lastName);
		$sqlQuery->set($registeration->email);
		$sqlQuery->set($registeration->phone);
		$sqlQuery->set($registeration->cityName);
		$sqlQuery->setNumber($registeration->stateId);
		$sqlQuery->setNumber($registeration->countryId);
		$sqlQuery->set($registeration->address);
		$sqlQuery->set($registeration->fax);
		$sqlQuery->set($registeration->company);
		$sqlQuery->set($registeration->photo);
		$sqlQuery->setNumber($registeration->zip);
		$sqlQuery->setNumber($registeration->userId);
		$sqlQuery->setNumber($registeration->createdBy);
		$sqlQuery->set($registeration->createDate);
		$sqlQuery->setNumber($registeration->updatedBy);
		$sqlQuery->set($registeration->updated_date);
		$sqlQuery->setNumber($registeration->status);

		$sqlQuery->setNumber($registeration->registerId);
		return $this->executeUpdate($sqlQuery);
	}

	/**
 	 * Delete all rows
 	 */
	public function clean(){
		$sql = 'DELETE FROM registeration';
		$sqlQuery = new SqlQuery($sql);
		return $this->executeUpdate($sqlQuery);
	}

	public function queryByFirstName($value){
		$sql = 'SELECT * FROM registeration WHERE first_name = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByLastName($value){
		$sql = 'SELECT * FROM registeration WHERE last_name = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByEmail($value){
		$sql = 'SELECT * FROM registeration WHERE email = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByPhone($value){
		$sql = 'SELECT * FROM registeration WHERE phone = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByCityName($value){
		$sql = 'SELECT * FROM registeration WHERE city_name = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByStateId($value){
		$sql = 'SELECT * FROM registeration WHERE state_id = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->getList($sqlQuery);
	}

	public function queryByCountryId($value){
		$sql = 'SELECT * FROM registeration WHERE country_id = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->getList($sqlQuery);
	}

	public function queryByAddress($value){
		$sql = 'SELECT * FROM registeration WHERE address = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByFax($value){
		$sql = 'SELECT * FROM registeration WHERE fax = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByCompany($value){
		$sql = 'SELECT * FROM registeration WHERE company = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByPhoto($value){
		$sql = 'SELECT * FROM registeration WHERE photo = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByZip($value){
		$sql = 'SELECT * FROM registeration WHERE zip = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->getList($sqlQuery);
	}

	public function queryByUserId($value){
		$sql = 'SELECT * FROM registeration WHERE user_id = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->getList($sqlQuery);
	}

	public function queryByCreatedBy($value){
		$sql = 'SELECT * FROM registeration WHERE created_by = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->getList($sqlQuery);
	}

	public function queryByCreateDate($value){
		$sql = 'SELECT * FROM registeration WHERE create_date = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByUpdatedBy($value){
		$sql = 'SELECT * FROM registeration WHERE updated_by = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->getList($sqlQuery);
	}

	public function queryByUpdatedDate($value){
		$sql = 'SELECT * FROM registeration WHERE updated_date = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByStatus($value){
		$sql = 'SELECT * FROM registeration WHERE status = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->getList($sqlQuery);
	}


	public function deleteByFirstName($value){
		$sql = 'DELETE FROM registeration WHERE first_name = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByLastName($value){
		$sql = 'DELETE FROM registeration WHERE last_name = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByEmail($value){
		$sql = 'DELETE FROM registeration WHERE email = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByPhone($value){
		$sql = 'DELETE FROM registeration WHERE phone = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByCityName($value){
		$sql = 'DELETE FROM registeration WHERE city_name = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByStateId($value){
		$sql = 'DELETE FROM registeration WHERE state_id = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByCountryId($value){
		$sql = 'DELETE FROM registeration WHERE country_id = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByAddress($value){
		$sql = 'DELETE FROM registeration WHERE address = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByFax($value){
		$sql = 'DELETE FROM registeration WHERE fax = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByCompany($value){
		$sql = 'DELETE FROM registeration WHERE company = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByPhoto($value){
		$sql = 'DELETE FROM registeration WHERE photo = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByZip($value){
		$sql = 'DELETE FROM registeration WHERE zip = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByUserId($value){
		$sql = 'DELETE FROM registeration WHERE user_id = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByCreatedBy($value){
		$sql = 'DELETE FROM registeration WHERE created_by = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByCreateDate($value){
		$sql = 'DELETE FROM registeration WHERE create_date = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByUpdatedBy($value){
		$sql = 'DELETE FROM registeration WHERE updated_by = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByUpdatedDate($value){
		$sql = 'DELETE FROM registeration WHERE updated_date = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByStatus($value){
		$sql = 'DELETE FROM registeration WHERE status = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->executeUpdate($sqlQuery);
	}


	
	/**
	 * Read row
	 *
	 * @return RegisterationMySql 
	 */
	protected function readRow($row){
		$registeration = new Registeration();
		
		$registeration->registerId = $row['register_id'];
		$registeration->firstName = $row['first_name'];
		$registeration->lastName = $row['last_name'];
		$registeration->email = $row['email'];
		$registeration->phone = $row['phone'];
		$registeration->cityName = $row['city_name'];
		$registeration->stateId = $row['state_id'];
		$registeration->countryId = $row['country_id'];
		$registeration->address = $row['address'];
		$registeration->fax = $row['fax'];
		$registeration->company = $row['company'];
		$registeration->photo = $row['photo'];
		$registeration->zip = $row['zip'];
		$registeration->userId = $row['user_id'];
		$registeration->createdBy = $row['created_by'];
		$registeration->createDate = $row['create_date'];
		$registeration->updatedBy = $row['updated_by'];
		$registeration->updated_date = $row['updated_date'];
		$registeration->status = $row['status'];

		return $registeration;
	}
	
	protected function getList($sqlQuery){
		$tab = QueryExecutor::execute($sqlQuery);
		$ret = array();
		for($i=0;$i<count($tab);$i++){
			$ret[$i] = $this->readRow($tab[$i]);
		}
		return $ret;
	}
	
	/**
	 * Get row
	 *
	 * @return RegisterationMySql 
	 */
	protected function getRow($sqlQuery){
		$tab = QueryExecutor::execute($sqlQuery);
		if(count($tab)==0){
			return null;
		}
		return $this->readRow($tab[0]);		
	}
	
	/**
	 * Execute sql query
	 */
	protected function execute($sqlQuery){
		return QueryExecutor::execute($sqlQuery);
	}
	
		
	/**
	 * Execute sql query
	 */
	protected function executeUpdate($sqlQuery){
		return QueryExecutor::executeUpdate($sqlQuery);
	}

	/**
	 * Query for one row and one column
	 */
	protected function querySingleResult($sqlQuery){
		return QueryExecutor::queryForString($sqlQuery);
	}

	/**
	 * Insert row to table
	 */
	protected function executeInsert($sqlQuery){
		return QueryExecutor::executeInsert($sqlQuery);
	}
}
?>